package com.pg.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pg.entity.EipTemp;
import com.pg.entity.dd.EipProductionOrder;
import com.pg.entity.dd.EipPurchaseOrder;
import com.pg.entity.dd.EipSalesOrder;
import com.pg.entity.dd.OrderView;
import com.pg.mapper.EipProductionOrderMapper;
import com.pg.mapper.EipPurchaseOrderMapper;
import com.pg.mapper.EipSalesOrderMapper;
import com.pg.mapper.EipTempMapper;
import com.pg.service.EipSalesOrderService;
import com.pg.utils.ColumnUtil;
import com.pg.utils.DataStringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;

/**
 * <p>
 * 销售订单 服务实现类
 * </p>
 *
 * @author likl
 * @since 2020-07-06
 */
@Service
@DS("proxyDs")
@Primary
public class EipSalesOrderServiceImpl extends ServiceImpl<EipSalesOrderMapper, EipSalesOrder> implements EipSalesOrderService {

    @Autowired
    private EipTempMapper eipTempMapper;

    @Autowired
    private EipPurchaseOrderMapper eipPurchaseOrderMapper;

    @Autowired
    private EipSalesOrderMapper eipSalesOrderMapper;

    @Autowired
    private EipProductionOrderMapper eipProductionOrderMapper;

    @Override
    public void updateSalesOrder(EipTemp eipTemp) {
        LambdaQueryWrapper<EipTemp> eipTempQueryWrapper = new LambdaQueryWrapper<>();
        // 采购订单号
        if (eipTemp.getLinkPUID() != null) {
            eipTempQueryWrapper.eq(EipTemp::getLinkPUID, eipTemp.getLinkPUID());
        }
        EipTemp eipTempByLinkPuid = eipTempMapper.selectOne(eipTempQueryWrapper);
        if (eipTempByLinkPuid == null) {
            eipTemp.setPuid(UUID.randomUUID().toString());
            eipTempMapper.insert(eipTemp);
        } else {
            eipTemp.setPuid(eipTempByLinkPuid.getPuid());
            eipTempMapper.updateByPuid(eipTemp);
        }
        EipSalesOrder eipSalesOrderTemp = new EipSalesOrder();
        eipSalesOrderTemp.setIoflag("mod");
        LambdaQueryWrapper<EipSalesOrder> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EipSalesOrder::getPuid, eipTemp.getLinkPUID());
        eipSalesOrderMapper.update(eipSalesOrderTemp, queryWrapper);
    }

    @Override
    public void logicalDelete(String puid) {
        eipSalesOrderMapper.logicalDelete(puid);
    }

    @Override
    public void logicalDeleteBatch(List<String> asList) {
        for (String puid : asList) {
            eipSalesOrderMapper.logicalDelete(puid);
        }
    }

    /**
     * 更加行项目puid查询订单
     *
     * @param puid
     * @return
     */
    @Override
    public EipSalesOrder selectByPuid(String puid) {
        EipSalesOrder eipSalesOrder = eipSalesOrderMapper.selectByPuid(puid);
        eipSalesOrder.setRemark1(selectWarnMessage(eipSalesOrder));
        return eipSalesOrder;
    }

    @Override
    public List<OrderView> selectListByMap(Map<String, String> map) {
        return eipSalesOrderMapper.selectListByMap(map.get("modifyTime"), map.get("ioflag"));
    }

    @Override
    public List<EipSalesOrder> selectForList(Page<EipSalesOrder> page, EipSalesOrder eipSalesOrder) {
        List<EipSalesOrder> eipSalesOrderListTemp = new ArrayList<>();
        String eipPoNo = null;
        String poItemNo = null;
        if (eipSalesOrder.getEipPoNo() != null) {
            String[] eipPoNos = eipSalesOrder.getEipPoNo().split("\\|");
            if (eipPoNos.length == 1) {
                eipPoNo = eipPoNos[0];
            }
            if (eipPoNos.length == 2) {
                eipPoNo = eipPoNos[0];
                poItemNo = eipPoNos[1];
            }
        }
        List<EipSalesOrder> eipSalesOrderList = eipSalesOrderMapper.selectForList(page, eipSalesOrder.getPurchaserHqCode(), eipSalesOrder.getPuid(),
                eipSalesOrder.getSoNo(), eipSalesOrder.getSupplierCode(), eipSalesOrder.getSubclassCode(),
                eipSalesOrder.getProductName(), eipSalesOrder.getBuyerName(), eipSalesOrder.getConCode(), eipPoNo);
        for (EipSalesOrder eipSalesOrderTemp : eipSalesOrderList) {
            if (eipSalesOrder.getEipPoNo() != null) {
                String[] eipPoNos = eipSalesOrderTemp.getEipPoNo().split("&");
                String eipPoNo3 = null;
                for (String eipPoNo2 : eipPoNos) {
                    if ("1".equals(eipSalesOrderTemp.getSupplierSoRelType())) {
                        eipPoNo3 = eipPoNo;
                    } else if ("2".equals(eipSalesOrderTemp.getSupplierSoRelType())) {
                        eipPoNo3 = eipPoNo + "|" + poItemNo;
                    }
                    if (eipPoNo2.equals(eipPoNo3) || eipPoNo2.contains(eipPoNo3)) {
                        String soStatus = getSoStatus(eipSalesOrderTemp.getSoNo());
                        eipSalesOrderTemp.setSoStatus(soStatus);
                        String simpleName = DataStringUtil.getSimpleName(eipSalesOrder.getProductName());
                        eipSalesOrderTemp.setProductName(simpleName);
                        eipSalesOrderTemp.setRemark1(selectWarnMessage(eipSalesOrderTemp));
                        eipSalesOrderListTemp.add(eipSalesOrderTemp);
                    }
                }
            } else {
                String soStatus = getSoStatus(eipSalesOrderTemp.getSoNo());
                eipSalesOrderTemp.setSoStatus(soStatus);
                String simpleName = DataStringUtil.getSimpleName(eipSalesOrderTemp.getProductName());
                eipSalesOrderTemp.setProductName(simpleName);
                eipSalesOrderTemp.setRemark1(selectWarnMessage(eipSalesOrderTemp));
                eipSalesOrderListTemp.add(eipSalesOrderTemp);
            }
        }
        return eipSalesOrderListTemp;
    }

    private String selectWarnMessage(EipSalesOrder eipSalesOrder) {
        boolean flag = true;
        String poNo = "";
        String poItemNo = "";
        String eipPoNo = eipSalesOrder.getEipPoNo();
        if (eipPoNo != null && !"".equals(eipPoNo)) {
            String[] poNos = eipPoNo.split("&");
            // 遍历采购订单号
            for (String no : poNos) {
                poNo = no;
                Map<String, Object> map = new HashMap<String, Object>();
                if ("1".equals(eipSalesOrder.getSupplierSoRelType())) {
                    map.put(ColumnUtil.getName(EipPurchaseOrder::getPoNo), poNo);
                }
                if ("2".equals(eipSalesOrder.getSupplierSoRelType())) {
                    String[] poNoAndpoItemIds = poNo.split("\\|");
                    if (poNoAndpoItemIds.length == 2) {
                        // 采购订单号
                        poNo = poNoAndpoItemIds[0];
                        // 采购订单行项目号
                        poItemNo = poNoAndpoItemIds[1];
                        map.put(ColumnUtil.getName(EipPurchaseOrder::getPoNo), poNo);
                        map.put(ColumnUtil.getName(EipPurchaseOrder::getPoItemNo), poItemNo);
                    }
                }
                List<EipPurchaseOrder> eipPurchaseOrders = eipPurchaseOrderMapper.selectByMap(map);
                if (!CollectionUtils.isEmpty(eipPurchaseOrders)) {
                    flag = false;
                }
            }
        }
        return flag ? "10001" : "";
    }

    private String getSoStatus(String soNo) {
        String soStatus = "1";
        Map<String, Object> map = new HashMap<>();
        map.put(ColumnUtil.getName(EipProductionOrder::getPoItemId), soNo);
        map.put(ColumnUtil.getName(EipProductionOrder::getIsDeleted), '0');
        List<EipProductionOrder> eipProductionOrders = eipProductionOrderMapper.selectByMap(map);
        for (EipProductionOrder eipProductionOrder : eipProductionOrders) {
            soStatus = eipProductionOrder.getIpoStatus();
            if ("5".equals(eipProductionOrder.getIpoStatus())) {
                soStatus = "5";
            }
            if ("3".equals(eipProductionOrder.getIpoStatus())) {
                soStatus = "3";
                break;
            }
        }
        return soStatus;
    }
}
